package com.tanhua.admin.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.service.AnalysisService;
import com.tanhua.model.admin.Analysis;

import com.tanhua.model.vo.AnalysisSummaryVo;
import com.tanhua.model.vo.CountVo;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/dashboard")
public class DashBoardController {

    @Autowired
    private AnalysisService analysisService;

    /**
     * 概要统计信息
     */
    @GetMapping("/summary")
    public AnalysisSummaryVo getSummary() {

        AnalysisSummaryVo analysisSummaryVo = new AnalysisSummaryVo();

        Date now = Calendar.getInstance().getTime();

        //累计用户数
        Integer total = analysisService.queryCumulativeUsers();
        analysisSummaryVo.setCumulativeUsers(Long.valueOf(total));

        //查询今日统计信息
        Analysis today_analysis = analysisService.querySummary(now);

        //过去30天活跃用户
        Integer days=30;
        Integer getNumActive30=analysisService.queryNumActive30(days);
        analysisSummaryVo.setActivePassMonth(Long.valueOf(getNumActive30));
        Integer days1=7;
        //过去7天活跃用户
        Integer getNumActive7=analysisService.queryNumActive30(days1);
        analysisSummaryVo.setActivePassWeek(Long.valueOf(getNumActive7));

        //今日活跃用户
        String type="num_active";
        Integer dayCount=analysisService.querytoday(type);
        analysisSummaryVo.setActiveUsersToday(Long.valueOf(dayCount));
        //昨日
        Integer lastCount=analysisService.querylasttoday(type);
        BigDecimal bigDecimal = DashBoardController.computeRate(Long.valueOf(dayCount.toString()), Long.valueOf(lastCount.toString()));
        analysisSummaryVo.setActiveUsersTodayRate(bigDecimal);

        //今日新增用户
        String type1="num_register";
        Integer dayCount1=analysisService.querytoday(type1);
        System.out.println(dayCount1);
        analysisSummaryVo.setNewUsersToday(Long.valueOf(dayCount1));
        //昨日
        Integer lastCount1=analysisService.querylasttoday(type1);
        System.out.println(lastCount1);
        BigDecimal bigDecimal1 = DashBoardController.computeRate(Long.valueOf(dayCount1.toString()), Long.valueOf(lastCount1.toString()));
        analysisSummaryVo.setNewUsersTodayRate(bigDecimal1);
        //今日登录次数
        String type2="num_login";
        Integer dayCount2=analysisService.querytoday(type2);
        analysisSummaryVo.setLoginTimesToday(Long.valueOf(dayCount2));
        //昨日
        Integer lastCount2=analysisService.querylasttoday(type2);
        BigDecimal bigDecimal2 = DashBoardController.computeRate(Long.valueOf(dayCount2.toString()), Long.valueOf(lastCount2.toString()));
        analysisSummaryVo.setLoginTimesTodayRate(bigDecimal2);


        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌

        //查询昨日统计信息
        Analysis yes_analysis = analysisService.querySummary(DateUtils.addDays(now, -1));

        analysisSummaryVo.setNewUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumRegistered()), Long.valueOf(yes_analysis.getNumRegistered())));
        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setLoginTimesTodayRate(computeRate(Long.valueOf(today_analysis.getNumLogin()), Long.valueOf(yes_analysis.getNumLogin())));

        //活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        analysisSummaryVo.setActiveUsersTodayRate(computeRate(Long.valueOf(today_analysis.getNumActive()), Long.valueOf(yes_analysis.getNumActive())));

        return analysisSummaryVo;
    }

    private static BigDecimal computeRate(Long current, Long last) {
        BigDecimal result;
        if (last == 0) {
            // 当上一期计数为零时，此时环比增长为倍数增长
            result = new BigDecimal((current - last) * 100);
        } else {
            result = BigDecimal.valueOf((current - last) * 100).divide(BigDecimal.valueOf(last), 2, BigDecimal.ROUND_HALF_DOWN);
        }
        return result;
    }

    private static String offsetDay(Date date, int offSet) {
        return DateUtil.offsetDay(date, offSet).toDateStr();
    }

    @GetMapping("/users")
    public ResponseEntity users(Long sd, Long ed, Integer type) {
        String startTime=new SimpleDateFormat("yyyyMMdd").format(new Date(sd));
        String endTime=new SimpleDateFormat("yyyyMMdd").format(new Date(ed));
        CountVo countVo = analysisService.getCount(startTime, endTime, type);
        return ResponseEntity.ok(countVo);
    }
}